/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.utbm.tx52.stamp.ui.actionprovider;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ActionProvider;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import fr.utbm.tx52.stamp.R;
import fr.utbm.tx52.stamp.logic.DataManager;
import fr.utbm.tx52.stamp.ui.activity.Main;
import java.util.Calendar;

/**
 * Action provider used in the main activity when clicking on the button new
 * carte postale
 *
 * @author GREA Antoine
 * @author TISSERAND Geoffrey
 */
public class PhotoSource extends ActionProvider implements MenuItem.OnMenuItemClickListener
{

	private Activity activity;

	public PhotoSource(Context activity)
	{
		super(activity);
		this.activity = (Activity) activity;
	}

	@Override
	public View onCreateActionView()
	{
		Log.d(this.getClass().getSimpleName(), "onCreateActionView");
		return null;
	}

	@Override
	public boolean onPerformDefaultAction()
	{
		Log.d(this.getClass().getSimpleName(), "onPerformDefaultAction");
		return super.onPerformDefaultAction();
	}

	@Override
	public boolean hasSubMenu()
	{
		Log.d(this.getClass().getSimpleName(), "hasSubMenu");
		return true;
	}

	@Override
	public void onPrepareSubMenu(SubMenu subMenu)
	{
		Log.d(this.getClass().getSimpleName(), "onPrepareSubMenu");
		subMenu.clear();
		subMenu.add(0, 0, 0, R.string.take_picture).setOnMenuItemClickListener(this).setIcon(R.drawable.ic_menu_light_camera);
		subMenu.add(0, 1, 1, R.string.choose_in_gallery).setOnMenuItemClickListener(this).setIcon(R.drawable.ic_menu_light_picture);
	}

	@Override
	public boolean onMenuItemClick(MenuItem item)
	{
		Intent intent;
		switch (item.getItemId())
		{
			case 0: // Take picture
				intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				Calendar rightNow = Calendar.getInstance();
				intent.putExtra(MediaStore.EXTRA_OUTPUT, DataManager.DATA_PATH + "/" + Calendar.YEAR + "." + Calendar.MONTH + Calendar.DAY_OF_MONTH + ".jpg");
				activity.startActivityForResult(intent, Main.TAKE_PICTURE);
				break;
			case 1: // Choose in gallery
				intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				activity.startActivityForResult(intent, Main.CHOOSE_IN_GALLERY);
				break;
		}
		return true;
	}
}
